* This dofiles produces Tables 3-7 and Appendix Table 13 and Figure 10

global Granted "Granted"
global tmp "/tmp"
cd ../data


* -----------------------------------------------------------------------------
* REGRESSIONS
* -----------------------------------------------------------------------------
use reg_variables6c${Granted}_WTO, clear

log using ../logs/results_final, replace

set more off

global yy "L8lnP"
global x "L8taut"
global c "ncty lnP85 homew"
global vce "vce(cluster id)"
global iff "MissTar==0 & homew<1 & wto1995==1"

gen L8tautExPred = L8tautPred/(1-homewPred)
gen L8tautExAppPred = L8tautAppPred/(1-homewPred)


* -----------------------------------------------------------------------------
* Table 3, Appendix Table 13 and Appendix Figure 10
* -----------------------------------------------------------------------------
* Main result - symmetric model
xtivreg $yy (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first 	
xtivreg $yy $c (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first  	
xtivreg $yy $c L8lnPall (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first
gen esample = e(sample) == 1
xtivreg LLlnP $c LLlnPall (LLtautApp = LLtaut) if y==2004 & $iff, fe i(id) $vce first  	

* First stage
xtreg L8tautApp L8taut $c L8lnPall if y==2000 & $iff, fe i(id) $vce 
xtreg L8tautApp L8taut $c L8lnPall if y==2000 & $iff & esample==1, fe i(id) $vce 

* Reduced form
xtreg $yy L8taut $c L8lnPall if y==2000 & $iff, fe i(id) $vce 

* Binscatter
binscatter $yy L8taut if y==2000 & $iff, controls($c) absorb(id) ///
    xtitle("{&Delta}T_bar{sub:i}", size(12pt)) ytitle("{&Delta}lnK{sub:i}", size(12pt)) ///
    xlabel(,labsize(12pt)) ylabel(,labsize(12pt))
graph export ../graph/binscatter_2019.pdf, replace
graph export ../graph/binscatter_2019.eps, replace

* Back of the envelope exercise
sum L8lnP L8lnC L8tautApp L8taut if y==2000 & $iff


* -----------------------------------------------------------------------------
* Table 4
* -----------------------------------------------------------------------------
* Patent quality

* Citations
xtivreg L8lnCbar $c L8lnPall (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first

* Research team
xtivreg L8lnIbar $c L8lnPall (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first  	

* IPC codes
xtivreg L8lnIPCbar $c L8lnPall (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first  	

* Number of markets (family size)
xtivreg L8lnMbar $c L8lnPall (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first  	


* -----------------------------------------------------------------------------
* Table 5
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
* Robustness
* -----------------------------------------------------------------------------
global c "ncty lnP85 homew L8lnPall"

* Country-level tariffs
xtreg $yy L8taus $c if y==2000 & $iff, fe i(id) $vce

* Triadic patents
xtivreg L8lnPtri $c (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first

* Fixed effects for destination
merge m:1 hrm using weightswic5d_reshape_to1985, keep(match master)
foreach v of varlist w?? {
  gen Dum`v' = `v'>0
}
xtivreg $yy ncty lnP85 Dumw?? (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first  	
xtivreg $yy ncty lnP85 Dumw?? (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) first  	

* Import tariffs
xtivreg $yy $c L8lnPall L8tautAppImp (L8tautApp = L8taut) if y==2000 & $iff & Missrta==0 & MissTarhq==0, fe i(id) $vce first

* Import and input tariffs
xtivreg $yy $c L8lnPall L8tautAppImp L8tautInpAppImp (L8tautApp = L8taut) if y==2000 & $iff & Missrta==0 & MissTarhq==0 & MissInpTarhq==0, fe i(id) $vce first

* # patents during period as dependent variable
tsset  hrm y
gen ch2 = log(P-l8.P)
xtivreg ch2 $c (L8tautApp = L8taut) if y==2000 & $iff, fe i(id) $vce first	


* -----------------------------------------------------------------------------
* Placebo - regress patenting in the 1980s on trade liberalization in the 1990s
* -----------------------------------------------------------------------------
global yyear 1988 // Final patent year 

use tauT5d_to1975, clear

* Drop firms that are patenting in countries with missing tariff data
keep hrm headq nace homew ncty taut1992-taut2000  tautApp1992-tautApp2000 Miss*
reshape long taut tautApp, i(hrm headq homew ncty) j(y)
replace taut = taut/100
save $tmp/tmpplacebo, replace

use lnPall_to1975, clear
keep hrm lnPall1980-lnPall1990
reshape long lnPall, i(hrm) j(y)
save $tmp/lnPalltmp, replace

use $tmp/tmpplacebo, clear
replace y = y-(2000-$yyear)  // Lag year 12 years
keep if y<=$yyear
merge 1:1 hrm y using DDplacebo$Granted
keep if _merge==3
gen lnP = log(P)
gen lnP75 = log(P75)
drop _merge

merge 1:1 hrm y using $tmp/lnPalltmp, keep(match)
drop _merge

merge m:1 headq using WTOmembers_sample${Granted}, keep(match master)
drop _merge

tsset hrm y
capture drop L8*
foreach vv of varlist p P ln* tau* cit* {
  gen L8`vv' = `vv' - l8.`vv'
}

egen id = group(nace2_1 headq)
xtivreg L8lnP ncty lnP75 L8lnPall homew (L8tautApp = L8taut) if $iff & y==$yyear, fe i(id) vce(robust) first  


* -----------------------------------------------------------------------------
* Heterogeneity results
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
* Tables 6 and 7
* -----------------------------------------------------------------------------
di "****************** Sec 5: Country heterogeneity results ******************"
use heterogeneity_variables, clear

set more off

global yy "L8lnP"
global z "L8taut"
global x "L8tautApp"
global c "ncty lnP85 homew"
global cc "homew L8lnPall"
global vce "vce(cluster id)"
global iff "MissTar==0 & homew<1"

gen L8tautExPred = L8tautPred/(1-homewPred)
gen L8tautExAppPred = L8tautAppPred/(1-homewPred)

// Country heterogeneity: GDP per capita quartiles
xtivreg $yy $c L8lnPall ($x ${x}_gdp_pcap_q2 ${x}_gdp_pcap_q3 ${x}_gdp_pcap_q4 = $z ${z}_gdp_pcap_q2 ///
    ${z}_gdp_pcap_q3 ${z}_gdp_pcap_q4) if y==2000 & $iff, fe i(id) $vce first 	

// Country heterogeneity: trade (% of GDP) quartiles 
xtivreg $yy $c L8lnPall ($x ${x}_trade_q2 ${x}_trade_q3 ${x}_trade_q4 = $z ${z}_trade_q2 ///
    ${z}_trade_q3 ${z}_trade_q4) if y==2000 & $iff, fe i(id) $vce first

// Country heterogeneity: Country's ex-ante patent stock 
xtivreg $yy $c L8lnPall ($x ${x}_Pctry_q2 ${x}_Pctry_q3 ${x}_Pctry_q4 = $z ${z}_Pctry_q2 ///
    ${z}_Pctry_q3 ${z}_Pctry_q4) if y==2000 & $iff, fe i(id) $vce first

// Firm level heterogemeity: Ex-ante firm size
// Quartiles of firm size (within industry and country)
xtivreg $yy $c L8lnPall i.size1_quart ($x ${x}_size1_q2 ${x}_size1_q3 ${x}_size1_q4 = $z ///
    ${z}_size1_q2 ${z}_size1_q3 ${z}_size1_q4) if y==2000 & $iff, fe i(id) $vce first
forvalues i = 2/4 {
    test $x ${x}_size1_q`i'
}
test ${x}_size1_q2 ${x}_size1_q3
test ${x}_size1_q2 ${x}_size1_q4
test ${x}_size1_q3 ${x}_size1_q4

xtivreg $yy $c L8lnPall i.size3_quart ($x ${x}_size3_q2 ${x}_size3_q3 ${x}_size3_q4 = $z ///
    ${z}_size3_q2 ${z}_size3_q3 ${z}_size3_q4) if y==2000 & $iff, fe i(id) $vce first  	
forvalues i = 2/4 {
    test $x ${x}_size3_q`i'
}
test ${x}_size3_q2 ${x}_size3_q3
test ${x}_size3_q2 ${x}_size3_q4
test ${x}_size3_q3 ${x}_size3_q4

log close
